package com.xykj.enterprise.wechat.event.service;

import com.xykj.enterprise.wechat.bean.ext.contacts.member.UserListVo;
import com.xykj.enterprise.wechat.bean.ext.contacts.member.UserVo;
import com.xykj.enterprise.wechat.core.dao.CorpExtDao;
import com.xykj.enterprise.wechat.core.dao.UserDao;
import com.xykj.enterprise.wechat.core.model.dodb.CorpExt;
import com.xykj.enterprise.wechat.core.model.dodb.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author zhouxu
 * @create 2021-04-06 21:56
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private CorpExtDao corpExtDao;

    @Override
    public void syncUserList(UserListVo listVo, String corpid) {
        if (listVo != null && !listVo.getUserlist().isEmpty()) {
            for (UserVo vo : listVo.getUserlist()) {
                User user = userDao.get(vo.getUserid(), corpid);
                if (user == null) {
                    userDao.save(
                            corpid,
                            vo.getUserid(),
                            vo.getName(),
                            vo.getMobile(),
                            vo.getDepartment().toString(),
                            vo.getOrder().toString(),
                            vo.getPosition(),
                            vo.getGender(),
                            vo.getEmail(),
                            vo.getIs_leader_in_dept().toString(),
                            vo.getAvatar(),
                            vo.getThumb_avatar(),
                            vo.getTelephone(),
                            vo.getAlias(),
                            vo.getStatus(),
                            vo.getQr_code(),
                            vo.getExternal_profile(),
                            vo.getAddress(),
                            vo.getOpen_userid(),
                            vo.getMain_department()
                    );
                } else {
                    userDao.update(
                            user.getId(),
                            corpid,
                            vo.getUserid(),
                            vo.getName(),
                            vo.getMobile(),
                            vo.getDepartment().toString(),
                            vo.getOrder().toString(),
                            vo.getPosition(),
                            vo.getGender(),
                            vo.getEmail(),
                            vo.getIs_leader_in_dept().toString(),
                            vo.getAvatar(),
                            vo.getThumb_avatar(),
                            vo.getTelephone(),
                            vo.getAlias(),
                            vo.getStatus(),
                            vo.getQr_code(),
                            vo.getExternal_profile(),
                            vo.getAddress(),
                            vo.getOpen_userid(),
                            vo.getMain_department()
                    );
                }
            }
        }

        // 同步完成标记
        corpExtDao.syncUserData(corpid, CorpExt.SUCC);
    }

}
